package com.vtb.thread;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vtb.entity.Config;
import com.vtb.entity.DataCache;
import com.vtb.utils.mail.MailSenderInfo;
import com.vtb.utils.mail.SimpleMailSender;

/**
 * 异常邮件发送
 * @author tacheno
 *
 */
public class MailSendThread implements Runnable {
	private static final Log log = LogFactory.getLog(MailSendThread.class);
	public void run() {
		log.info("邮件发送线程已启动");
		while (true) {
			SimpleMailSender sms = new SimpleMailSender();
			String message;
			MailSenderInfo mailInfo = new MailSenderInfo();
			try {
				message = DataCache.mailQeque.take();
				mailInfo.setMailServerHost(Config.MAIL_SERVER_HOST);
				mailInfo.setMailServerPort("25");
				mailInfo.setValidate(true);
				mailInfo.setUserName(Config.MAIL_USER_NAME);
				mailInfo.setPassword(Config.MAIL_PASSWORD);
				mailInfo.setFromAddress(Config.MAIL_FROM);
				mailInfo.setSubject("微同步异常");
				for (int i = 0; i < Config.MAIL_RECEIVERS.length; i++) {
					log.debug("异常邮件正在准备发送。。。。。。。。。。。。。。。。。");
					mailInfo.setToAddress(Config.MAIL_RECEIVERS[i]);
					mailInfo.setContent(message);
					sms.sendTextMail(mailInfo);
					log.debug("异常邮件已发送给-----"+Config.MAIL_RECEIVERS[i]);
				}
			} catch (Exception e) {
				log.error("发送异常邮件失败：", e);
			}
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
